package com.xxx.video.guide.views;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.xxx.video.R;
import com.xxx.video.VideoApplication;
import com.xxx.video.commen.events.OpenPermissionEvent;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.commen.views.BaseDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */
public class AppUpdateTipDialog extends BaseDialog {
    @BindView(R.id.iv_dialog_app_update_tip)
    ImageView ivDialogAppUpdateTip;
    @BindView(R.id.tv_dialog_app_update_no_update)
    TextView tvDialogAppUpdateNoUpdate;
    @BindView(R.id.tv_dialog_app_update_update)
    TextView tvDialogAppUpdateUpdate;
    @BindView(R.id.tv_dialog_app_update_bottom_tip)
    TextView tvDialogAppUpdateBottomTip;
    String[] permissions = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"
    };

    private OnAppUpdateClickListener onAppUpdateClickListener;
    private String downloadUrl;

    public AppUpdateTipDialog(@NonNull Context context, String downloadUrl) {
        super(context);
        this.downloadUrl = downloadUrl;
    }

    @Override
    public int getContentLayout() {
        return R.layout.dialog_app_update_tip;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView(View view) {

    }

    public void setOnAppUpdateClickListener(OnAppUpdateClickListener onAppUpdateClickListener) {
        this.onAppUpdateClickListener = onAppUpdateClickListener;
    }

    public interface OnAppUpdateClickListener {
        /***
         * update click
         * @param downloadUrl download url
         */
        void onConfirmClick(String downloadUrl);
    }

    @OnClick({R.id.tv_dialog_app_update_no_update, R.id.tv_dialog_app_update_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_dialog_app_update_no_update:
                dismiss();
                break;
            case R.id.tv_dialog_app_update_update:
                if (ContextCompat.checkSelfPermission(VideoApplication.getInstance(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(VideoApplication.getInstance(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    OpenPermissionEvent openPermissionEvent = new OpenPermissionEvent();
                    openPermissionEvent.permissions = permissions;
                    EventBus.getDefault().post(openPermissionEvent);
                } else {
                    if (onAppUpdateClickListener != null) {
                        onAppUpdateClickListener.onConfirmClick(downloadUrl);
                    }
                    dismiss();
                }
                break;
            default:
                break;
        }
    }
}
